package com.ydkj.a37e_mall.Classes.Acitivity.BaseActivity;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageView;

import com.ydkj.a37e_mall.R;

/**
 * Created by Administrator on 2017/3/5.
 */

public class ProductStore extends BaseActivity {
    private Activity mActivity;
    private WebView wv_activity;
    private String url;
    private ImageView img_back;
    ProgressDialog progDlg = null;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.product_store);
        mActivity = this;
        init();
//        initWindow();//沉浸式状态栏
    }

    @SuppressLint("JavascriptInterface")
    private void init() {
        progDlg = new ProgressDialog(mActivity);
        if (this.getIntent().getExtras() != null) {
            url = this.getIntent().getExtras().getString("url");
            Log.e("url", "url==" + url);
        }
        img_back = (ImageView) this.findViewById(R.id.img_back);
        img_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        wv_activity = (WebView) mActivity.findViewById(R.id.wv_activity);
        wv_activity.addJavascriptInterface(new JavascriptInterface(mActivity), "android");
        wv_activity.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
        // 设置WebView属性，能够执行Javascript脚本
        wv_activity.getSettings().setJavaScriptEnabled(true);
        // 设置可以支持缩放
        wv_activity.getSettings().setSupportZoom(true);
        // 设置出现缩放工具
        wv_activity.getSettings().setBuiltInZoomControls(true);
        //扩大比例的缩放
        wv_activity.getSettings().setUseWideViewPort(true);
        //自适应屏幕
        wv_activity.getSettings().setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
        wv_activity.getSettings().setLoadWithOverviewMode(true);
        // 加载需要显示的网页
        wv_activity.loadUrl(url);
        // 设置Web视图
        wv_activity.setWebViewClient(new MyWebViewClient());
    }

    public class JavascriptInterface {
        private Context context;

        public JavascriptInterface(Context context) {
            this.context = context;
        }
    }

    // 监听 所有点击的链接，如果拦截到我们需要的，就跳转到相对应的页面。
    private class MyWebViewClient extends WebViewClient {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            if (url != null && url.contains("id:")) {
//                Intent intent = new Intent(ActivityWebView.this, EnrollTryToPlay.class);
//                intent.putExtra("url", url);
//                Log.e("url", "url==" + url);
//                ActivityWebView.this.startActivity(intent);
//                finish();
                return true;
            }
            return super.shouldOverrideUrlLoading(view, url);
        }

        public void onPageStarted(WebView view, String url, Bitmap favicon) {
            if (progDlg == null || !progDlg.isShowing()) {
                progDlg.setMessage("正在加载，请稍候...");
            }
            progDlg.show();
        }

        @Override
        public void onPageFinished(WebView view, String url) {
            view.getSettings().setJavaScriptEnabled(true);
            super.onPageFinished(view, url);
            progDlg.dismiss();
        }
    }


    // 覆盖Activity类的onKeyDown(int keyCoder,KeyEvent event)方法
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK && wv_activity.canGoBack()) {
            wv_activity.goBack();// 返回前一个页面
//            finish();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        progDlg.dismiss();
    }
}
